package com.h.files.selector.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.h.files.selector.R;
import com.h.files.selector.activity.fragment.FileSelectorFragment;
import com.h.files.selector.activity.fragment.ImageSelectorFragment;
import com.h.files.selector.activity.util.BitmapUtils;

@SuppressLint("NewApi")
public class MultiFileSelectorActivity extends FragmentActivity {

	private List<String> resultList = new ArrayList<String>();
	/*
	 * 获取返回数据key
	 */
	public static final String INTENT_RESULT_KEY = "multiFileResultData";
	/*
	 * 设置文件选择类型，0是图片，1是文件，2是远程
	 */
	public static final String INTENT_REQUEST_KEY_TYPE = "multiFileRequestType";
	/*
	 * 设置文件最大选择数量
	 */
	public static final String INTENT_REQUEST_KEY_MAX_NUM = "multiFileRequestMaxNum";
	/*
	 * 是否显示拍照只有当INTENT_REQUEST_KEY_TYPE=0的时候生效
	 */
	public static final String INTENT_REQUEST_KEY_SHOWCAMERA = "multiFileRequestCamera";
	// 默认值
	private int intent_request_key_type = 0;
	private int intent_request_key_max_num = 9;
	private boolean intent_request_key_showCamera = true;

	private TextView back;
	private Button submit;
	private RadioGroup group;
	private FrameLayout content;
	private List<Fragment> fragments = new ArrayList<Fragment>();

	/**
	 * 打开文件选择器。
	 * 
	 * @param activity
	 *            哪个activity需要选择文件
	 * @param requestCode
	 * @param type
	 *            设置文件选择类型，0是图片，1是文件，2是远程
	 * @param maxNum
	 *            最大选择数量
	 * @param isShowCamera
	 *            是否显示拍照
	 */
	public static void startSelector(Activity activity, int requestCode, int type, int maxNum, boolean isShowCamera) {
		Intent intent = new Intent(activity, MultiFileSelectorActivity.class);
		intent.putExtra(INTENT_REQUEST_KEY_TYPE, type);
		intent.putExtra(INTENT_REQUEST_KEY_MAX_NUM, maxNum);
		intent.putExtra(INTENT_REQUEST_KEY_SHOWCAMERA, isShowCamera);
		activity.startActivityForResult(intent, requestCode);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.a_multi_file_selector);
		initWidget();
		initListener();
		initData();
	}

	private void initWidget() {
		BitmapUtils.init(this);
		
		back = (TextView)this.findViewById(R.id.w_top_tv_back);
		submit = (Button)this.findViewById(R.id.w_top_btn_submit);
		group = (RadioGroup)this.findViewById(R.id.a_multi_bar);
		content  = (FrameLayout)this.findViewById(R.id.a_multi_content);

		Intent intent = getIntent();
		intent_request_key_type = intent.getIntExtra(INTENT_REQUEST_KEY_TYPE, 0);
		intent_request_key_max_num = intent.getIntExtra(INTENT_REQUEST_KEY_MAX_NUM, 9);
		intent_request_key_showCamera = intent.getBooleanExtra(INTENT_REQUEST_KEY_SHOWCAMERA, true);

		fragments.add(new ImageSelectorFragment());
		fragments.add(new FileSelectorFragment());

	}

	private void initListener() {
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				Object tag = group.findViewById(checkedId).getTag();
				int index = Integer.parseInt(tag + "");
				addOrShow(index);
			}
		});
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setNewResult(Activity.RESULT_OK);
			}
		});
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setNewResult(Activity.RESULT_CANCELED);
			}
		});
	}

	private void initData() {
		setSelectedNum(0);
		((RadioButton)group.getChildAt(intent_request_key_type)).setChecked(true);
		addOrShow(intent_request_key_type);
	}

	private void addOrShow(int index) {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		Fragment fragment = fragments.get(index);
		ft.replace(R.id.a_multi_content, fragment);
		ft.commit();
	}

	public void setNewResult(int resultOk) {
		Intent data = new Intent();
		if (resultOk == Activity.RESULT_OK){
			data.putExtra(INTENT_RESULT_KEY, resultList.toArray(new String[] {}));
			data.putStringArrayListExtra(INTENT_RESULT_KEY, (ArrayList<String>) resultList);
		}
		setResult(RESULT_OK, data);
		finish();
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK)
			return true;
		return super.dispatchKeyEvent(event);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		for (Fragment f : fragments) {
			if (f.isVisible()) {
				f.onActivityResult(requestCode, resultCode, data);
				break;
			}
		}
	}

	public void setSelectedNum(int num) {
		if (0 <= num) {
			submit.setText("完成(" + num + "/" + intent_request_key_max_num + ")");
		}
	}

	public List<String> getResultList() {
		return resultList;
	}

	public int getIntent_request_key_max_num() {
		return intent_request_key_max_num;
	}

	public boolean isIntent_request_key_showCamera() {
		return intent_request_key_showCamera;
	}

}
